<template>
  <div>
    <form>
      <el-button type="primary" style="margin-top: 20px;" @click="visible=true">添加区域</el-button>
      <br>
      <el-table
        v-loading="loading"
        :data="data"
        style="width: 100%"
        @row-click="onBack"
      >
        <el-table-column label="序号" type="index" prop="id">
          <template #default="{$index}">
            {{ $index + 1 + 10 *(page -1) }}
          </template>
        </el-table-column>
        <el-table-column label="区域名称" prop="areaName" />
        <el-table-column label="车位数(个)" prop="spaceNumber" />
        <el-table-column label="面积（㎡）" prop="areaProportion" />
        <el-table-column label="计费规则" prop="ruleName" />
        <el-table-column label="备注" prop="remark" />
        <el-table-column
          prop="demoFlag"
          label="操作"
        >
          <template #default="{row}">
            <el-button type="text" size="small" @click="onSub('edit' ,row.id)">编辑</el-button>
            <el-button type="text" size="small" @click="onSub('del' ,row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </form>

    <el-row type="flex" justify="end">
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20,50,100]"
        layout="total,prev,pager,next,sizes"
        :total="total"
        :size-change="page"
        :page-size="10"
        @size-change="onSizeChange"
        @current-change="onCurrentChange"
      />
    </el-row>

    <add-district ref="distRef" :visible.sync="visible" @onSuccess="onLoadPark" />
  </div>
</template>

<script>
import { getParkAPI } from '@/api/district'
import addDistrict from './components/add-district.vue'
export default {
  components: { addDistrict },
  data() {
    return {
      data: [],
      page: 1,
      pageSize: 10,
      total: 0,
      loading: false,
      visible: false
    }
  },
  created() {
    this.onLoadPark()
  },
  methods: {
    onBack(row) {
      console.log(row)
      this.$refs.distRef.formData = {
        areaName: row.areaName,
        areaProportion: row.areaProportion,
        spaceNumber: row.spaceNumber,
        id: row.id,
        ruleId: row.ruleId
      }
    },
    onSub(type, id) {
      if (type === 'edit') {
        this.visible = true
        this.$refs.distRef.formData.id = id
        this.$refs.distRef.onGetDetailPark(id)
      }
      if (type === 'del') {
        this.$refs.distRef.onDel(id)
      }
    },
    async onLoadPark() {
      this.loading = true
      const res = await getParkAPI({
        page: this.page,
        pageSize: this.pageSize })
      this.loading = false
      this.data = res.data.rows
      this.total = res.data.total
    },
    onCurrentChange(page) {
      this.page = page
      this.onLoadPark()
    },
    onSizeChange(page) {
      this.pageSize = page
      this.onLoadPark()
      this.page = 1
    }
  }
}
</script>

<style>

</style>
